package com.hnkywl.sts.action.report;

import com.hnkywl.sts.service.report.CwkcService;
import com.hnkywl.sts.service.report.CwsfcService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-14
 * Time: 下午4:48
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/report/cwkc")
public class CwkcAction extends SimpleActionSupport {
    private Long gsId;
    private Date date;
    private int page;
    private int rows;
    private String wzbm;
    private Integer wzdl;
    private CwkcService cwkcService;


    @Action("cwkc")
    public String cwsfc() {
        date = new Date();
        return SUCCESS;
    }

    @Action("cwkcList")
    public void cwkcList() {
        String json = cwkcService.cwkcList(gsId, date, wzbm, wzdl, page, rows);
        renderJson(json);
    }

    @Action("export")
    public String export() {
        Map<String, Map> map = cwkcService.cwkcListExport(gsId, date, wzbm, wzdl);
        List<Map> result = (List<Map>) map.get("rows");
        List<Map> footer = (List<Map>) map.get("footer");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "公司名称", "物资编号", "品名", "规格", "图号", "单位", "库存数量", "库存金额", "未开票数量", "未开票金额");
        List<Integer> scale = Arrays.asList(0, 0, 0, 0, 0, 0, 4, 2, 4, 2);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("gsjc"), item.get("code"), item.get("ggmc"), item.get("ggxh"),
                    item.get("th"), item.get("dw"), item.get("kcsl"), item.get("kcje"), item.get("wjscksl"),
                    item.get("wjsckje"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRowComplex(wb, sheet, sumStyle, result.size() + 1, scale, "合计：", "", "", "", "", "",
                footer.get(0).get("kcsl"), footer.get(0).get("kcje"), footer.get(0).get("wjscksl"), footer.get(0).get("wjsckje"));
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("财务库存.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }
    //=================setter getter==============

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getWzbm() {
        return wzbm;
    }

    public void setWzbm(String wzbm) {
        if (wzbm == null) {
            wzbm = "-1";
        }
        this.wzbm = wzbm;
    }

    public Integer getWzdl() {
        return wzdl;
    }

    public void setWzdl(Integer wzdl) {
        this.wzdl = wzdl;
    }

    //=================setter===================
    @Autowired
    public void setCwkcService(CwkcService cwkcService) {
        this.cwkcService = cwkcService;
    }
}
